# -*- coding: utf-8 -*-
import urllib2, re

CHANNEL = {'电影' : 'http://v.qq.com/movie/type/list_9_-1_1_0_1.html',
           '电视剧' : 'http://v.qq.com/tv/type/list_9_-1_1_0_1.html',
           '综艺' : 'http://v.qq.com/variety/index.html',
           '音乐' : 'http://v.qq.com/music/index.html',
           '娱乐' : 'http://v.qq.com/ent/index.html',
           '体育' : 'http://v.qq.com/sports/index.html',
           '新闻' : 'http://v.qq.com/news/index.html',
           '财经' : 'http://v.qq.com/finance/index.html'}

SOFT_FILTER = {'按更新':'0', '按热度':'1', '按评分':'2'}

SHOW_TYPE_FILTER = {'海报图':'0', '详情':'1'}

class Video:
    def __init__(self, title, link, img):
        self.title = title
        self.link = link
        self.img = img
        
    def toString(self):
        return 'Title = ' + self.title + ', Link = ' + self.link + ', Img = ' + self.img
    
def getHttpData(url):
    req = urllib2.Request(url)
    req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
    response = urllib2.urlopen(req)
    httpdata = response.read()
    response.close()
    return httpdata

def getHttpDataCompact(url):
    httpdata = getHttpData(url)
    httpdata = re.sub("\s", "", httpdata)
    return httpdata

def getTypeFilter(url):
    html = getHttpDataCompact(url)
    filters = {}
    match = re.compile('<ahref="list_1_\d+_1_0_1.html"class="[^"]*_gtag[^"]*"tvalue="([^"]+)"title="[^"]+">([^<]+)</a>').findall(html)
    index = 0;    
    while index < len(match):
        filters[match[index][1]] = match[index][0]
        index = index + 1
    return filters   

def getAreaFilter(url):
    html = getHttpDataCompact(url)
    filters = {}
    match = re.compile('<ahref="list_2_\d+_1_0_1.html"class="[^"]*_gtag[^"]*"tvalue="([^"]+)"title="[^"]+">([^<]+)</a>').findall(html)
    index = 0;    
    while index < len(match):
        filters[match[index][1]] = match[index][0]
        index = index + 1
    return filters

def getYearFilter(url):
    html = getHttpDataCompact(url)
    filters = {}
    match = re.compile('<ahref="list_3_\d+_1_0_1.html"class="[^"]*_gtag[^"]*"tvalue="([^"]+)"title="[^"]+">([^<]+)</a>').findall(html)
    index = 0;    
    while index < len(match):
        filters[match[index][1]] = match[index][0]
        index = index + 1
    return filters

def getVersionFilter(url):
    html = getHttpDataCompact(url)
    filters = {}
    match = re.compile('<ahref="list_7_\d+_1_0_1.html"class=""tvalue="([^"]+)"title="[^"]+">([^<]+)</a>').findall(html)
    index = 0;    
    while index < len(match):
        filters[match[index][1]] = match[index][0]
        index = index + 1
    return filters

def getArtistFilter(url):
    html = getHttpDataCompact(url)
    filters = {}
    match = re.compile('<ahref="list_4_\d+_1_0_1.html"class=""tvalue="([^"]+)"title="[^"]+">([^<]+)</a>').findall(html)
    index = 0;    
    while index < len(match):
        filters[match[index][1]] = match[index][0]
        index = index + 1
    return filters

def getVideos(url):
    html = getHttpDataCompact(url)
    videos = []
    match = re.compile('<divclass="mod_pic"><ahref="([^"]+)"class="mod_poster_v"><imgsrc="([^"]+)"onerror="picerr\(this\);"alt="([^"]+)"class="_tipimg"/></a></div>').findall(html)
    index = 0;   
    while index < len(match):
        if (match[index][2] != '${movie.title}'):
            video = Video(match[index][2], url + match[index][0], match[index][1])
            videos.append(video)
        index = index + 1
    return videos;

#filters = getArtistFilter('http://v.qq.com/movie/type/list_9_-1_1_0_1.html')
#for key in filters.keys():
#    print key

videos = getVideos('http://v.qq.com/movie/type/list_9_-1_1_0_1.html')
for video in videos:
    print video.toString()
